﻿using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AssettDatabase
{
    public partial class Form_Assets_ReportFilter : Form
    {
        public Form_Assets_ReportFilter()
        {
            InitializeComponent();
        }

        private void chkExpired_CheckedChanged(object sender, EventArgs e)
        {
            if (chkExpired.Checked)
            {
                dateFrom.Enabled = false;
                dateTo.Enabled = false;
            }
            else
            {
                dateFrom.Enabled = true;
                dateTo.Enabled = true;
            }
        }

        private void Btn_Return_Main_Click(object sender, EventArgs e)
        {
            this.Dispose();
            FormProvider.MainMenu.Show();
        }

        private void BT_Show_Report_Click(object sender, EventArgs e)
        {
            BLL.Asset bllAsset = new BLL.Asset();
            DataTable dtData = bllAsset.GetAssetsData(Convert.ToInt32(TB_RT_Customer_ID.Text), TB_SerialNumber.Text, TB_ModelNumber.Text, ((VO.Asset_Type)CB_Assett_Type.SelectedItem).Asset_Type_ID,
                                                    CB_Vendor_Name.SelectedText, ((VO.Supplier)CB_Supplier.SelectedItem).Supplier_ID, Convert.ToDateTime(dateFrom.Text), Convert.ToDateTime(dateTo.Text), chkExpired.Checked);
            dtData.TableName = "DataTable1";
            if (dtData.Rows.Count == 0)
                MessageBox.Show("No data available. Please check your filters.");
            DataTable1BindingSource.DataSource = dtData;
            reportViewer1.RefreshReport();
            reportViewer1.Visible = true;
        }

        private void Form_Assets_ReportFilter_Load(object sender, EventArgs e)
        {
            AutoCompleteText();
            fill_Asset_Type();
            fill_Vendor();
            fill_Supplier();
            reportViewer1.Visible = false;
        }

        void AutoCompleteText()
        {
            try
            {
                BLL.Customer bllCustomer = new BLL.Customer();
                AutoCompleteStringCollection strCollection = new AutoCompleteStringCollection();
                TB_Customer_Name.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                TB_Customer_Name.AutoCompleteSource = AutoCompleteSource.CustomSource;
                TB_Customer_Name.AutoCompleteCustomSource = strCollection;

                List<VO.Customer> voCustomerList = bllCustomer.GetCustomers();

                foreach (VO.Customer customer in voCustomerList)
                {
                    strCollection.Add(customer.Customer_Name);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AA_TB_Customer_Name_KeyDown(object sender, KeyEventArgs e)
        {
            if (TB_Customer_Name.Text.Length != 0)
                if (e.KeyCode == Keys.Enter)
                {
                    Customer_Search(null);
                }
        }

        private void Customer_Search(string Customer_ID)
        {
            try
            {
                BLL.Customer bllCustomer = new BLL.Customer();
                List<VO.Customer> voCustomerList;
                if (Customer_ID != null)
                    voCustomerList = bllCustomer.GetCustomerById(Customer_ID);
                else
                    voCustomerList = bllCustomer.GetCustomerByName(TB_Customer_Name.Text);

                foreach (VO.Customer customer in voCustomerList)
                {
                    TB_Customer_Name.Text = customer.Customer_Name.ToString();
                    TB_RT_Customer_ID.Text = customer.Customer_ID.ToString();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void fill_Asset_Type()
        {
            try
            {
                BLL.Asset_Type bllAsset_Type = new BLL.Asset_Type();
                List<VO.Asset_Type> voAsset_TypeList;
                voAsset_TypeList = bllAsset_Type.GetAsset_Type();
                //add an item for all asset type
                voAsset_TypeList.Add(new VO.Asset_Type { Asset_Type_ID = -1, Asset_Type_Name = "<select one>" });

                CB_Assett_Type.DataSource = voAsset_TypeList;
                CB_Assett_Type.DisplayMember = VO.Asset_Type.Asset_Type_Fields.Asset_Type_Name.ToString();
                CB_Assett_Type.ValueMember = VO.Asset_Type.Asset_Type_Fields.Asset_Type_ID.ToString();
                CB_Assett_Type.SelectedIndex = CB_Assett_Type.FindStringExact("<select one>");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void fill_Vendor()
        {
            try
            {
                BLL.Vendor bllVendor = new BLL.Vendor();
                List<VO.Vendor> voVendorList;
                voVendorList = bllVendor.GetVendor();
                //add an item for all vendors
                voVendorList.Add(new VO.Vendor { Vendor_ID = -1, Vendor_Name = "<select one>" });

                CB_Vendor_Name.DataSource = voVendorList;
                CB_Vendor_Name.DisplayMember = VO.Vendor.Vendor_Fields.Vendor_Name.ToString();
                CB_Vendor_Name.ValueMember = VO.Vendor.Vendor_Fields.Vendor_ID.ToString();
                CB_Vendor_Name.SelectedIndex = CB_Vendor_Name.FindStringExact("<select one>");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void fill_Supplier()
        {
            try
            {
                BLL.Supplier bllSupplier = new BLL.Supplier();
                List<VO.Supplier> voSupplierList;
                voSupplierList = bllSupplier.GetSupplier();
                //add an item for all suppliers
                voSupplierList.Add(new VO.Supplier { Supplier_ID = -1, Supplier_Name = "<select one>" });

                CB_Supplier.DataSource = voSupplierList;
                CB_Supplier.DisplayMember = VO.Supplier.Supplier_Fields.Supplier_Name.ToString();
                CB_Supplier.ValueMember = VO.Supplier.Supplier_Fields.Supplier_ID.ToString();
                CB_Supplier.SelectedIndex = CB_Supplier.FindStringExact("<select one>");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form_Assets_ReportFilter_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            FormProvider.MainMenu.Show();
        }

        private void AA_BT_Search_Customer_Click(object sender, EventArgs e)
        {
            using (var form = new Form_Sel_Customer())
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    Customer_Search(form.retCustomer_ID);
                }
            }
        }

        private void TB_Customer_Name_KeyDown(object sender, KeyEventArgs e)
        {
            if (TB_Customer_Name.Text.Length != 0)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    Customer_Search(null);
                }
            }
            else
                TB_RT_Customer_ID.Text = "-1";

        }

        private void reportViewer1_Load(object sender, EventArgs e)
        {

        }  
    }
}
